package com.weng.platform.stories.api;

import cn.hutool.core.convert.Convert;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.weng.framework.core.constants.ResultCodeEnum;
import com.weng.framework.core.model.ResponseData;
import com.weng.framework.web.base.BaseController;
import com.weng.framework.web.model.request.DeleteReqVo;
import com.weng.platform.stories.entity.Stories;
import com.weng.platform.stories.model.req.StoryProgressReq;
import com.weng.platform.stories.model.vo.StoriesTreeNode;
import com.weng.platform.stories.model.vo.StoriesVo;
import com.weng.platform.stories.service.IStoriesService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import java.util.List;


/**
 * 课时 前端控制器
 *
 * @author wenghui
 * @date 2020-05-17 13:16:40
 *
 */  
@Slf4j
@RestController
@RequestMapping("/stories")
@Api(value = "课时api", tags = {"课时操作接口"})
public class StoriesApi extends BaseController {
    @Autowired
    private IStoriesService storiesService;

    /**
     * 更新进度
     */
    @ApiOperation(value = "更新进度", notes = "")
    @PostMapping(value = "/set-progress")
    public ResponseData setProgress(@Valid @RequestBody StoryProgressReq storyProgressReq){
        for(String id : storyProgressReq.getIds()){
            Stories stories = storiesService.getById(id);
            if(stories == null){
                return ResponseData.failed("需求不存在，设置失败!");
            }
            if(storyProgressReq.getProgressWeb() != null){
                stories.setProgressWeb(storyProgressReq.getProgressWeb());
            }
            if(storyProgressReq.getProgressApi() != null){
                stories.setProgressApi(storyProgressReq.getProgressApi());
            }
            if(storyProgressReq.getProgressIntegrate() != null){
                stories.setProgressIntegrate(storyProgressReq.getProgressIntegrate());
            }
            storiesService.update(stories);
        }
        return ResponseData.success();
    }


    @ResponseBody
    @RequestMapping( value = "/get-tree-grid-data" ,method=RequestMethod.GET)
    @ApiOperation(value = "获取树形表格数据", notes = "获取树形表格数据")
    public ResponseData getTreeGridData(String name, String iterationId, String chapterTypes
            , String parentId, String isExpand, HttpServletRequest request) {
        String[] chapterTypesArray = null;
        if(StringUtils.isNotEmpty(chapterTypes)){
            chapterTypesArray = chapterTypes.split(",");
        }
        List<StoriesTreeNode> treeData = this.storiesService.getTreeGridData(name, iterationId, chapterTypesArray, parentId, isExpand);
        return ResponseData.success(treeData);
    }



    /**
     * 功能描述:单个获取课时
     */
    @ApiOperation(value = "获取课时", notes = "")
    @GetMapping(value = "/get")
    public ResponseData get(String id){
        Stories stories = storiesService.getById(id);
        return success(stories);
    }

    /**
     * 条件 分页查询课时
     */
    @ApiOperation(value = "分页获取课时", notes = "")
    @GetMapping(value = "/list")
    public ResponseData list(Integer pageNo,Integer pageSize){
		IPage<Stories> iPage = storiesService.list(pageNo, pageSize);
        return success(iPage);
    }

    /**
	 * 新增课时
	 */
	@ApiOperation(value = "新增课时", notes = "")
	@PostMapping(value = "/add")
	public ResponseData add(@Valid @RequestBody StoriesVo lessonPeriodReq){
		Stories stories = Convert.convert(Stories.class, lessonPeriodReq);
		return storiesService.add(stories);
	}

	/**
	 * 更新课时
	 */
	@ApiOperation(value = "更新课时", notes = "")
	@PostMapping(value = "/update")
	public ResponseData update(@Valid @RequestBody StoriesVo lessonPeriodReq){
		Stories stories = Convert.convert(Stories.class, lessonPeriodReq);
        if(stories.getParentId() != null && stories.getParentId().equals(stories.getId())){
            return ResponseData.failed(ResultCodeEnum.PARAM_VALID_FAIL.getCode(),"不能选择当前结点做为父结点！");
        }
		return storiesService.update(stories);
	}

	/**
	 * 删除课时
	 */
	@ApiOperation(value = "ids 删除课时", notes = "可以删除多个，中间以,号分隔")
	@PostMapping(value = "/delete")
	public ResponseData delete(@RequestBody DeleteReqVo deleteReq){
		String ids = deleteReq.getIds();
		if(StringUtils.isEmpty(ids)){
			return failed("删除失败,id不能为空！");
		}
        return storiesService.removeByIds(ids);
	}


}
